package slr.comment.portlet;

import java.io.File;
import java.util.List;
import java.util.ResourceBundle;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.EventRequest;
import javax.portlet.EventResponse;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import javax.portlet.ResourceRequest;
import javax.portlet.ResourceResponse;

import slr.comment.model.SlrDocumentTyp;
import slr.comment.portlet.ui.SlrExplorerTree;
import slr.comment.portlet.util.Lang;
import slr.comment.portlet.util.SLRConstants;
import slr.comment.service.SlrDocumentTypLocalServiceUtil;

import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portal.kernel.util.WebKeys;
import com.liferay.portal.model.User;
import com.liferay.portal.theme.ThemeDisplay;
import com.vaadin.Application;
import com.vaadin.data.util.BeanItemContainer;
import com.vaadin.terminal.gwt.server.PortletApplicationContext2;
import com.vaadin.terminal.gwt.server.PortletApplicationContext2.PortletListener;
import com.vaadin.ui.Button;
import com.vaadin.ui.Component;
import com.vaadin.ui.SplitPanel;
import com.vaadin.ui.Table;
import com.vaadin.ui.Window;
import com.vaadin.ui.Window.Notification;

/**
 * This is the mainApplication for SLR-Comment portlet
 * 
 * @author TransIT mPower Labs
 * 
 */
@SuppressWarnings("serial")
public class SlrCommentApplication extends Application {
	
	public static String contextPath;
	public static File filePath;

	@Override
	public void init() {
		setMainWindow(new Window());
		if (getContext() instanceof PortletApplicationContext2) {
			PortletApplicationContext2 ctx = (PortletApplicationContext2) getContext();
			ctx.addPortletListener(this, new SLRPortletListener());
		} else {
			getMainWindow().showNotification("Not Initialized !", Notification.TYPE_ERROR_MESSAGE);
		}
	}

	private class SLRPortletListener implements PortletListener {

		/**
		 * This method is used display the contents of the SLR-Comment portlet
		 * first time.
		 * 
		 * @param mainWindow
		 *            parameter of type Window
		 */
		private void createComponents(final Window mainWindow) {
			final ResourceBundle bundle = ResourceBundle.getBundle("content.Language", getLocale());
			@SuppressWarnings("unused")
			Lang language = new Lang(bundle, getLocale());
			mainWindow.addComponent(new Button("Refresh", new Button.ClickListener() {
				public void buttonClick(Button.ClickEvent event) {
					mainWindow.removeAllComponents();
					createComponents(mainWindow);
					mainWindow.showNotification(Lang.getKey("slr.refreshed.application"));
				}
			}));
			SplitPanel splitPanel = new SplitPanel(SplitPanel.ORIENTATION_HORIZONTAL);
			splitPanel.setSizeFull();
			splitPanel.setSplitPosition(25, SplitPanel.UNITS_PERCENTAGE);
			splitPanel.setLocked(true);
			splitPanel.setFirstComponent(new SlrExplorerTree(splitPanel));
			splitPanel.setHeight(400, SplitPanel.UNITS_PIXELS);
			splitPanel.setSecondComponent(addDefaultComponent());
			mainWindow.addComponent(splitPanel);
		}

		/**
		 * Default SLRDocumentType List is displayed When SLRComment portlet is
		 * loaded
		 * 
		 * @return Component of type Table
		 */
		private Component addDefaultComponent() {
			Table table = new Table();
			List<SlrDocumentTyp> docTypeList = null;
			try {
				docTypeList = SlrDocumentTypLocalServiceUtil.getSlrDocumentTyps(0,
						SlrDocumentTypLocalServiceUtil.getSlrDocumentTypsCount());
				
			} catch (Exception e) {
				_log.error(e.getMessage());
			}

			if (Validator.isNotNull(docTypeList)) {
				BeanItemContainer<SlrDocumentTyp> slrDocContainer = new BeanItemContainer<SlrDocumentTyp>(docTypeList);
				table.setContainerDataSource(slrDocContainer);
				table.setVisibleColumns(SLRConstants.SLRDOCUMENTTYP.split(","));
				table.setImmediate(true);
				table.setHeight("100%");
				table.setWidth("100%");
			}
			return table;
		}

		public void handleRenderRequest(RenderRequest request, RenderResponse response, Window window) {
			contextPath = request.getContextPath();
			ThemeDisplay themeDisplay = (ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY);
			User user = themeDisplay.getUser();
			setUser(user);
			Window mainWindow = new Window("SLRComment");
			filePath = getContext().getBaseDirectory();
			mainWindow.setHeight("100%");
			mainWindow.setWidth("100%");
			mainWindow.removeAllComponents();
			setMainWindow(mainWindow);
			createComponents(mainWindow);
			
		}

		public void handleActionRequest(ActionRequest request, ActionResponse response, Window window) {

		}

		public void handleEventRequest(EventRequest request, EventResponse response, Window window) {

		}

		public void handleResourceRequest(ResourceRequest request, ResourceResponse response, Window window) {

		}
	}
    private static Log _log = LogFactoryUtil.getLog(SlrCommentApplication.class);
}